<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="title">
  <h1>APIs</h1>
  <button
    [routerLink]="'./new'"
    mat-raised-button
    color="primary"
    aria-label="add-api"
    *gioPermission="{ anyOf: ['environment-api-c'] }"
    data-testid="api_list_addApi_button"
  >
    <mat-icon>add</mat-icon> Add API
  </button>
</div>

<gio-table-wrapper
  [searchLabel]="searchLabel"
  [length]="apisTableDSUnpaginatedLength"
  [filters]="filters"
  (filtersChange)="onFiltersChanged($event)"
  [paginationPageSizeOptions]="[25, 50, 100, 200]"
>
  <table mat-table matSort [dataSource]="apisTableDS" id="apisTable" aria-label="Apis table">
    <!-- Picture Column -->
    <ng-container matColumnDef="picture">
      <th mat-header-cell *matHeaderCellDef id="picture"></th>
      <td mat-cell *matCellDef="let element">
        <gio-avatar [src]="element.picture" [name]="element.name + ' ' + element.version" [size]="32" [roundedBorder]="true"></gio-avatar>
      </td>
    </ng-container>

    <!-- Display Name Column -->
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef mat-sort-header id="name">Name</th>
      <td mat-cell *matCellDef="let element" title="{{ element.name }} ({{ element.version }})">
        <a [routerLink]="'./' + element.id">{{ element.name }} ({{ element.version }})</a>
      </td>
    </ng-container>

    <!-- Display States Column -->
    <ng-container matColumnDef="states">
      <th mat-header-cell *matHeaderCellDef id="states">Status</th>
      <td mat-cell *matCellDef="let element">
        <mat-icon
          *ngIf="element.state === 'STARTED'"
          matTooltip="Started"
          class="states__api-started"
          size="20"
          svgIcon="gio:play-circle"
        ></mat-icon>
        <mat-icon
          *ngIf="element.state && element.state !== 'STARTED'"
          matTooltip="Stopped"
          class="states__api-not-started"
          size="20"
          svgIcon="gio:stop-circle"
        ></mat-icon>
        <mat-icon
          *ngIf="element.isNotSynced$ | async"
          matTooltip="API out of sync"
          class="states__api-is-not-synced"
          size="20"
          svgIcon="gio:refresh-cw"
        ></mat-icon>
        <mat-icon
          *ngIf="element.lifecycleState === 'PUBLISHED'"
          matTooltip="Published"
          class="states__api-published"
          size="20"
          svgIcon="gio:cloud-published"
        ></mat-icon>
        <mat-icon
          *ngIf="element.lifecycleState && element.lifecycleState !== 'PUBLISHED'"
          matTooltip="Unpublished"
          class="states__api-not-published"
          size="20"
          svgIcon="gio:cloud-unpublished"
        ></mat-icon>
        <mat-icon
          *ngIf="element.origin === 'KUBERNETES'"
          matTooltip="Kubernetes Origin"
          class="states__api-origin"
          size="20"
          svgIcon="gio:kubernetes"
        ></mat-icon>
        <span
          *ngIf="element.workflowBadge"
          [ngClass]="element.workflowBadge.class"
          class="states__api-workflow-badge"
          [matTooltip]="element.workflowBadge.text"
        >
          {{ element.workflowBadge.text }}
        </span>
      </td>
    </ng-container>

    <!-- Display Access Column -->
    <ng-container matColumnDef="access">
      <th mat-header-cell *matHeaderCellDef mat-sort-header id="access">Access</th>
      <td mat-cell *matCellDef="let element">
        <ng-container *ngIf="element.access?.length > 0; else noAccessBlock">
          <a>{{ element.access[0] }}</a>
          <span *ngIf="element.access?.length > 1" class="access__badge gio-badge-neutral"> {{ element.access.length - 1 }} more </span>
        </ng-container>
        <ng-template #noAccessBlock>
          <span class="access__message">No access with this configuration</span>
        </ng-template>
      </td>
    </ng-container>

    <!-- Display Tags Column -->
    <ng-container matColumnDef="tags">
      <th mat-header-cell *matHeaderCellDef id="tags">Tags</th>
      <td mat-cell *matCellDef="let element">
        <a>{{ element.tags }}</a>
      </td>
    </ng-container>

    <!-- Display Quality Score Column -->
    <ng-container matColumnDef="qualityScore" *ngIf="isQualityDisplayed">
      <th mat-header-cell *matHeaderCellDef id="qualityScore">Quality</th>
      <td mat-cell *matCellDef="let element">
        <span *ngIf="$any(element.qualityScore$ | async) as qualityScore" class="quality-score" [ngClass]="qualityScore.class">
          {{ qualityScore.score }}%
        </span>
      </td>
    </ng-container>

    <!-- Display Owner Column -->
    <ng-container matColumnDef="owner">
      <th mat-header-cell *matHeaderCellDef id="owner">Owner</th>
      <td mat-cell *matCellDef="let element">
        <a [matTooltip]="element.ownerEmail ?? null">{{ element.owner }}</a>
      </td>
    </ng-container>

    <!-- Display Version Column -->
    <ng-container matColumnDef="definitionVersion">
      <th mat-header-cell *matHeaderCellDef id="definitionVersion">Definition</th>
      <td mat-cell *matCellDef="let element">
        <span class="gio-badge-neutral">
          <mat-icon *ngIf="element.definitionVersion.icon" class="gio-left" [svgIcon]="element.definitionVersion.icon"></mat-icon>
          {{ element.definitionVersion.label | titlecase }}
        </span>
        <span class="gio-badge-neutral" *ngIf="element.listenerTypes?.length > 0 && element.listenerTypes.includes('TCP')"> TCP </span>
      </td>
    </ng-container>

    <!-- Display Visibility Column -->
    <ng-container matColumnDef="visibility">
      <th mat-header-cell *matHeaderCellDef id="visibility">Visibility</th>
      <td mat-cell *matCellDef="let element">
        <mat-icon class="visibility" size="20" [matTooltip]="element.visibility.label | titlecase">{{ element.visibility.icon }}</mat-icon>
      </td>
    </ng-container>

    <!-- Actions Column -->
    <ng-container matColumnDef="actions">
      <th mat-header-cell *matHeaderCellDef id="actions"></th>
      <td mat-cell *matCellDef="let element">
        <div class="actions__edit">
          <button
            *ngIf="!element.readonly"
            [routerLink]="'./' + element.id"
            mat-icon-button
            aria-label="Button to edit an API"
            matTooltip="Edit API"
            data-testid="api_list_edit_button"
          >
            <mat-icon>edit</mat-icon>
          </button>
          <button
            *ngIf="element.readonly"
            [routerLink]="'./' + element.id"
            mat-icon-button
            aria-label="Button to view API details"
            matTooltip="View API details"
          >
            <mat-icon svgIcon="gio:eye-empty"></mat-icon>
          </button>
        </div>
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns" data-testid="api_list_table_header"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns" data-testid="api_list_table_row"></tr>

    <!-- Row shown when there is no data -->
    <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
      <td
        *ngIf="!isLoadingData && apisTableDS.length === 0"
        class="mat-mdc-cell mdc-data-table__cell"
        [attr.colspan]="displayedColumns.length"
      >
        {{ 'There is no API (yet).' }}
      </td>
      <td *ngIf="isLoadingData" class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="displayedColumns.length">
        {{ 'Loading...' }}
      </td>
    </tr>
  </table>
</gio-table-wrapper>
